/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package logica.cliente;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import javax.servlet.RequestDispatcher;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import model.cliente.Cliente;
import model.cliente.ClienteDAO;

/**
 *
 * @author davi
 */
public class AtualizaCliente implements logica.Logica{

    @Override
    public void executa(HttpServletRequest req, HttpServletResponse res) throws Exception {
        
        Cliente c = new Cliente();
        
        c.setIdCliente(Integer.parseInt(req.getParameter("idcliente")));
        c.setNome(req.getParameter("nome"));
        String dt_nasc = req.getParameter("dtNasc");
        String dt_cad = req.getParameter("dtCadastro");
        
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        
        calendar.setTime(sdf.parse(dt_nasc));
                c.setDtNasc(new java.sql.Date(calendar.getTimeInMillis()));
        
        calendar.setTime(sdf.parse(dt_cad));
                c.setDtCadastro(new java.sql.Date(calendar.getTimeInMillis()));
        
                
        calendar =  Calendar.getInstance();
        
        c.setDtAtualizacao(new java.sql.Date(calendar.getTimeInMillis()));       
        
        
        
         try{
        ClienteDAO dao = new ClienteDAO();
        dao.atualizar(c);
        }
        catch(Exception e ){
        
        e.printStackTrace();
        RequestDispatcher rd = req.getRequestDispatcher("erro.jsp");
        rd.forward(req, res);
        }
        
        req.setAttribute("cliente", c);
        req.setAttribute("operacao", "atualizacao");
        RequestDispatcher rd = req.getRequestDispatcher("sucesso.jsp");
        rd.forward(req, res);
        
    }
    
}
